//
//  XMCellToolsBar.m
//  XMWeibo
//
//  Created by chi on 14-8-26.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMCellToolsBar.h"
#import "XMStatus.h"

@interface XMCellToolsBar ()

/** 转发 */
@property (nonatomic, weak) UIButton *repostsBtn;

/** 评论 */
@property (nonatomic, weak) UIButton *commentsBtn;

/** 赞 */
@property (nonatomic, weak) UIButton *attitudesBtn;

/** 按钮数组 */
@property (nonatomic, strong) NSMutableArray *buttons;

/** 分割线数组 */
@property (nonatomic, strong) NSMutableArray *dividers;

@end

@implementation XMCellToolsBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 设置背景
        self.image = [UIImage stretchableImageWithName:@"timeline_card_bottom_background"];
        self.highlightedImage = [UIImage stretchableImageWithName:@"timeline_card_bottom_background_highlighted"];
        
        // 创建按钮
        self.repostsBtn = [self setuButtonWithTitle:@"转发" backgroundImage:@"timeline_card_leftbottom_highlighted" andIconImage:@"timeline_icon_retweet"];
        
        self.commentsBtn = [self setuButtonWithTitle:@"评论" backgroundImage:@"timeline_card_middlebottom_highlighted" andIconImage:@"timeline_icon_comment"];
        
        self.attitudesBtn = [self setuButtonWithTitle:@"赞" backgroundImage:@"timeline_card_rightbottom_highlighted" andIconImage:@"timeline_icon_unlike"];
        
        // 创建分割线
        [self setupDivider];
        [self setupDivider];
        
        
        self.userInteractionEnabled = YES;
    }
    return self;
}


- (void)setStatus:(XMStatus *)status
{

    [self.repostsBtn setTitle:[self stringByCount:status.reposts_count] forState:UIControlStateNormal];
    
    [self.commentsBtn setTitle:[self stringByCount:status.comments_count] forState:UIControlStateNormal];
    
    [self.attitudesBtn setTitle:[self stringByCount:status.attitudes_count] forState:UIControlStateNormal];
}

#pragma mark - 处理显示数量
- (NSString*)stringByCount:(NSUInteger)count
{
    if (count < 10000) {
        return [NSString stringWithFormat:@"%d", count];
    }
    
    CGFloat fCount = count / 10000.0;
    
    NSString *retStr = [NSString stringWithFormat:@"%0.1f万", fCount];
    
    return [retStr stringByReplacingOccurrencesOfString:@".0" withString:@""];
}

#pragma mark - 响应点击
- (void)clickButton:(UIButton*)button
{
    XMLog(@"clickbutton come here");
}

#pragma mark - 创建分割线
- (void)setupDivider
{
    UIImageView *divider = [[UIImageView alloc]init];
    divider.image = [UIImage adaptIOS7ImageWithName:@"timeline_card_bottom_line"];
    divider.highlightedImage = [UIImage adaptIOS7ImageWithName:@"timeline_card_bottom_line_highlighted"];
    
    [self addSubview:divider];
    
    [self.dividers addObject:divider];
}

#pragma mark - 创建按钮
- (UIButton*)setuButtonWithTitle:(NSString*)title backgroundImage:(NSString*)bkImage andIconImage:(NSString*)iconImage
{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage stretchableImageWithName:bkImage] forState:UIControlStateHighlighted];
    [button setImage:[UIImage adaptIOS7ImageWithName:iconImage] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    
    [button setAdjustsImageWhenHighlighted:NO];
    [self addSubview:button];
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [button setImageEdgeInsets:UIEdgeInsetsMake(-2.0, 0.0, 0.0, 5.0)];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;

    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:12.0];
    
    [self.buttons addObject:button];
    
    return button;
}

#pragma mark - 调整子视图位置

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat viewH = self.bounds.size.height;
    CGFloat dividerW = 2.0;
    
    CGFloat btnW = (self.bounds.size.width - dividerW * 2) / 3;
    CGFloat btnX = 0.0;
    for (int i = 0; i < self.buttons.count; ++i) {
        UIButton *btn = self.buttons[i];
        
        btnX = (btnW + dividerW) * i;
        
        btn.frame = CGRectMake(btnX, 0.0, btnW, viewH);
    }
    
    CGFloat dividerX = 0.0;
    for (int i = 0; i < self.dividers.count; ++i) {
        UIImageView *imgView = self.dividers[i];
        dividerX = btnW * (i + 1) + i * dividerW;
        imgView.frame = CGRectMake(dividerX, 0.0, dividerW, viewH);
    }
    
}


- (NSMutableArray *)dividers
{
    if (_dividers == nil) {
        _dividers = [NSMutableArray array];
    }
    
    return _dividers;
}

- (NSMutableArray *)buttons
{
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    
    return _buttons;
}

@end
